<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\LuckMoneyTimeRule;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class LuckMoneyTimeRuleController extends Controller
{
    use HasResourceActions;
    private $name = '红包单次规则';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new LuckMoneyTimeRule);

        $grid->id('ID');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(LuckMoneyTimeRule::findOrFail($id));

        $show->id('Id');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = null)
    {
        $ruleId = request()->input('rule_id', 0);
        $form = new Form(new LuckMoneyTimeRule);

        if ($ruleId != 0) {
            $form->hidden('rule_id')->default($ruleId);
        }

        $form->number('start', '开始次数')->rules('required');
        $form->number('end', '结束次数')->rules('required');
        $form->number('lower', '下限金额(分)')->rules('required');
        $form->number('upper', '上限金额(分)')->rules('required');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        $form->saved(function (Form $form) {
            admin_toastr(trans('admin.update_succeeded'));
            return redirect(admin_base_path('luckmoneyrule/' . $form->model()->rule_id));
        });

        return $form;
    }
}
